package com.mind.ridge.service.backend.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mind.ridge.mapper.backend.EmpMapper;
import com.mind.ridge.pojo.backend.Employee;
import com.mind.ridge.service.backend.EmpService;
import com.mind.ridge.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;

/**
 * @author Mind
 */
@Service
@Slf4j
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Employee> implements EmpService {

    @Override
    public R<Employee> login(HttpServletRequest request, Employee employee) {
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));

        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        //根据用户名查询
        lqw.eq(Employee::getUsername, employee.getUsername());
        Employee one = this.getOne(lqw);
        //查询结果为空则返回
        if (one == null) {
            return R.error("没有该用户！！");
        }
        //校验密码
        if (!password.equals(one.getPassword())) {
            return R.error("密码错误");
        }
        //校验该用户是否已被禁用
        if (one.getStatus() == 0) {
            return R.error("账号已禁用");
        }
        //将该用户的 id 保存到session中
        HttpSession session = request.getSession();
        session.setAttribute("id", one.getId());
        return R.success(one);
    }

    @Override
    public R logout(HttpServletRequest request) {
        //退出时将session中的数据移除
        request.getSession().removeAttribute("id");
        return R.success("退出成功！");
    }

    @Override
    public R<String> addEmp(HttpServletRequest request, Employee employee) {

        //判断用户是否为空
        if (employee == null) {
            return R.error("无用户信息！");
        }

        //设置一个默认的密码
        String psw = "123456";

        //md5加密
        psw = DigestUtils.md5DigestAsHex(psw.getBytes(StandardCharsets.UTF_8));

        //设置密码
        employee.setPassword(psw);

      /*  //设置当前时间为创建时间和修改时间
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());*/

     /*   //获取登陆时在session中存放的id
        long id = (long) request.getSession().getAttribute("id");

        //将id设置为改用的创建人和修改人
        employee.setCreateUser(id);
        employee.setUpdateUser(id);*/

        //调用save方法将该对象存放到数据库
        boolean res = this.save(employee);
        if (!res) {
            return R.error("添加失败！");
        }
        return R.success("添加成功！");
    }

    @Override
    public R<IPage> page(int page, int pageSize, String name) {

        //创建IPage对象，将前端传来的当前页数和页面大小传入构造器
        IPage<Employee> employeePage = new Page<>(page, pageSize);

/*        //判断用户是否在输入框输入姓名
        if(name == null || name.length() == 0) {
            this.page(employeePage);
            //将查询到的数据封装为R对象传给前端处理
            return R.success(employeePage);
        }

        //若用户输入了姓名则进行模糊分页查询
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();

        //调用like方法，传入字段名和 模糊查询值
        lqw.like(Employee::getName,name);

        //调用条件分页查询方法
        this.page(employeePage,lqw);*/

        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();


        lqw.like(Strings.isNotEmpty(name), Employee::getName, name);

        //添加排序(按照修改时间排序)
        lqw.orderByDesc(Employee::getUpdateTime);

        //条件分页查询
        this.page(employeePage, lqw);

        //返回数据
        return R.success(employeePage);
    }

    @Override
    public R<String> updateEmp(HttpServletRequest request, Employee employee) {
        if (employee == null) {
            return R.error("失败");
        }

        //获取当前操作用户的id
        Long id = (Long) request.getSession().getAttribute("id");

        //设置该用户的修改用户信息，和更新时间
        employee.setUpdateUser(id);
        employee.setUpdateTime(LocalDateTime.now());
        log.info(employee.toString());
        boolean b = this.updateById(employee);
        return b ? R.success("修改成功") : R.error("此用户已被删除或不存在,请刷新重试!");

    }

    @Override
    public R<Employee> echoInfo(String id) {
        log.info(id);
        if (!Strings.isNotEmpty(id)) {
            return R.error("ID为空");
        }
        //通过id查询
        Employee emp = this.getById(id);

        return emp != null ? R.success(emp) : R.error("此用户已被删除或不存在,请刷新重试!");
    }

}
